package com.ellricky.ui.activity

import android.content.Intent
import android.view.View
import com.andr.common.ui.activity.BaseActivity
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.ToastUtil
import com.ellricky.R
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.BaseConstant.*
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.SchoolEntry
import com.ellricky.baselib.entity.UserBean
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEntityCallback
import com.ellricky.checkLoginInput
import com.ellricky.consultant.ui.ConsultantMainActivity
import com.ellricky.excute_president.ui.activity.PresidentMainActivity
import com.ellricky.market.ui.MarketMainActivity
import com.ellricky.tc_manager.ui.TeachManagerMainActivity
import com.ellricky.teacher.ui.TeacherMainActivity
import kotlinx.android.synthetic.main.activity_login.*

class LoginActivity : BaseActivity<Void, BasePresenter<Void>>() {

    private var fromMain = false

    override fun getContentLayout(): Int {
        return R.layout.activity_login
    }

    override fun initUIView() {

        fromMain = intent.getBooleanExtra("fromMain", false)
        if (fromMain) LocalCacheUtil.removeLocalUser()

        val localUser = LocalCacheUtil.readLocalUser()
        if (localUser != null) {
            jumpMainPage(localUser.U_role)
            return
        }
        btnLogin.setOnClickListener(this)
    }

    override fun onClickView(v: View?) {
        super.onClickView(v)
        if (v == btnLogin) {
            val mobile = etMobile.text.toString().trim()
            val pwd = etPsd.text.toString().trim()
            if (checkLoginInput(this, mobile, pwd)) {
                login(mobile, pwd)
            }
        }
    }

    private fun login(tel: String, pwd: String) {
        val url = EAppUtil.getApiHost()
        val paramMap = mutableMapOf(
                "action" to ACTION_LOGIN,
                "tel" to tel,
                "pwd" to pwd
        )

        HttpUtils.getInstance().postFrom(url, paramMap, object : NewEntityCallback<UserBean>(this) {

            override fun onSuccess(data: UserBean, msg: String) {
                LocalCacheUtil.saveUser(data)
                jumpMainPage(data.U_role)
            }
        }, this)
    }

    private fun jumpMainPage(cardType: String?) {
        var intent: Intent? = null
        when (cardType) {
            CARD_PRESIDENT -> intent = Intent(this, PresidentMainActivity::class.java)
            CARD_TEACHER_MANAGER -> intent = Intent(this, TeachManagerMainActivity::class.java)
            CARD_TEACHER -> intent = Intent(this, TeacherMainActivity::class.java)
            CARD_MARKET -> intent = Intent(this, MarketMainActivity::class.java)
            CARD_CONSULTANT -> intent = Intent(this, ConsultantMainActivity::class.java)
            else -> ToastUtil.show(this, "无法识别的cardType")
        }

        intent?.let {
            startActivity(it)
            finish()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpUtils.getInstance().cancelTag(this)
    }
}

